package com.kfc.dao;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.kfc.entity.Teacher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

// 具体招聘来的采购
public class TeacherDaoImpl implements TeacherDao{
    @Override
    public int deleteTeacherById(int id) {
        // 真实从数据库拿数据，通过druid连接池
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        DruidDataSource dataSource = context.getBean(DruidDataSource.class);
        DruidPooledConnection conn = null;
        try {
            conn = dataSource.getConnection();
            Statement st = conn.createStatement();
            int i = st.executeUpdate("delete from student01 where id =" + id);
            return i;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public Teacher selectOneById(int id) {

        // 这个采购，会去超市采购对应的食材
        // 假设 一个数组是一个超市
        Teacher[] teachers = {new Teacher(1,"张三"),new Teacher(2,"李四")};

        // 真实从数据库拿数据，通过druid连接池
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        DruidDataSource dataSource = context.getBean(DruidDataSource.class);
        try {
            DruidPooledConnection conn = dataSource.getConnection();
            Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery("select * from mybatis.teacher where teacher_id=" + id);
            while (rs.next()){
                Teacher teacher = new Teacher(rs.getInt("teacher_id"), rs.getString("teacher_name"));
                return teacher;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return null;
    }
}
